﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using WorldServer.Models;
using System;
using WorldServer.Data;
using WorldServer.Services;
using WorldServer.DAO;
using WorldServer.Packets.Server;
using AionCxxLibrary.Enums;

namespace WorldServer.Commands
{
    [CxxSyntax("Syntax: .add <PlayerName> <ItemID|ItemName> [Count]")]
    public class Add : CxxCommand<Add>
    {
        protected override void Execute(Character sender, string[] parameters)
        {
            // .add Xita test_item_str_1 1
            if (parameters.Length < 2)
                throw new SyntaxException();
            string targetName = parameters[0];
            string item = parameters[1].ToLower();
            int itemId;
            long count = 1;

            Character target = LevelService.GetCharacter(targetName);

            if (target == null)
                throw new Exception("Player doesn't exist or is not online.");

            try
            {
                itemId = int.Parse(item);
            }
            catch (Exception)
            {
                try
                {
                    itemId = Datastore.ItemsByName[item].ID;
                }
                catch (Exception)
                {
                    throw new Exception("This item doesn't exist.");
                }
            }

            if (parameters.Length > 2)
            {
                try
                {
                    count = long.Parse(parameters[2]);
                }
                catch (Exception)
                {
                    throw new Exception("Invalid count.");
                }
            }

            if (!Datastore.ItemsById.ContainsKey(itemId))
                throw new Exception("This item doesn't exist.");

            ItemService.AddItem(target, itemId, count, true);

        }
    }

    [CxxSyntax("Syntax: .learn <PlayerName> <SkillID> [Level]")]
    public class Learn : CxxCommand<Learn>
    {
        protected override void Execute(Character sender, string[] parameters)
        {
            if (parameters.Length < 2)
                throw new SyntaxException();
            string playerName = parameters[0];

            Character target = LevelService.GetCharacter(playerName);

            if (target == null)
                throw new Exception("Target player not found.");

            int skillId;
            short level = 1;
            try
            {
                skillId = int.Parse(parameters[1]);
                if (parameters.Length > 2)
                    level = short.Parse(parameters[2]);
            }
            catch (Exception)
            {
                throw new Exception("Invalid SkillID or Level.");
            }

            target.SkillList.Add(new SkillEntry() { SkillID = skillId, Level = level });
            
        }
    }

    [CxxSyntax("Syntax: .set <level> <Value>")]
    public class Set : CxxCommand<Set>
    {
        protected override void Execute(Character sender, string[] parameters)
        {
            if (parameters.Length < 2)
                throw new SyntaxException();
            if (sender.Target == null)
                throw new Exception("You must select a target.");
            if (!(sender.Target is Character))
                throw new Exception("You must target a character.");
            Character target = sender.Target as Character;
            switch (parameters[0])
            {
                case "level":
                    {
                        byte level = byte.Parse(parameters[1]);
                        target.Template.Level = level;
                        CharacterDAO.UpdateLevelAndExp(target.GetGUID(), target.Template.Experience, target.Template.ExperienceRecoverable, target.Template.Level);
                        SPkt_CharacterDetails pkt = new SPkt_CharacterDetails(target);
                        target.NearbyObjects.BroadcastPacket(pkt, true);
                        target.SendPacket(new SPkt_CharacterStats());
                        break;
                    }
                case "class":
                    {
                        CharacterClass newclass = (CharacterClass)Enum.Parse(typeof(CharacterClass), parameters[1], true);
                        target.Template.Class = newclass;
                        CharacterDAO.UpdateClass(target.GetGUID(), (byte)target.Template.Class);
                        SPkt_CharacterDetails pkt = new SPkt_CharacterDetails(target);
                        target.NearbyObjects.BroadcastPacket(pkt, true);
                        break;
                    }
            }

        }
    }

    [CxxSyntax("Syntax: .gob <add> [ObjectID]")]
    public class GameObject : CxxCommand<GameObject>
    {
        protected override void Execute(Character sender, string[] parameters)
        {
            if (parameters.Length < 1)
                throw new SyntaxException();
            switch (parameters[0])
            {
                case "add":
                    if (parameters.Length < 2)
                        throw new SyntaxException();

                    NPC npc = new NPC(int.Parse(parameters[1]));
                    npc.Spawn(sender.Position, sender.LevelInstance);

                    sender.LevelInstance.RecalculateNearbyObjects(sender);

                    sender.SendMessage("spawned " + npc.Template.Name);

                    break;
                default:
                    throw new Exception("No such gob-operation: " + parameters[0]);
            }
        }
    }

}
